//
//  CookingViewController.m
//  美食百科
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 doll. All rights reserved.
//

#import "CookingViewController.h"
#import "NewWorking.h"
#import "ClassModel.h"
#import "CookCollectionViewCell.h"
#import "StapleViewController.h"
#import "Reachability.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height

@interface CookingViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,retain)UICollectionView *cookView;
@property(nonatomic,retain)NSMutableArray *cookArray;
@end

@implementation CookingViewController
- (void)dealloc
{   [_leftString release];
    [_urlString release];
    [_cookView release];
    [_cookArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        Reachability *reach = [Reachability reachabilityForInternetConnection];
        
        //如果无网络,弹窗提示
        if ( 0 == reach.currentReachabilityStatus){
            //弹窗提示
            UILabel *nonconnectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, kWidth, 50)];
            nonconnectionLabel.textAlignment = NSTextAlignmentCenter;
            nonconnectionLabel.backgroundColor = [UIColor redColor];
            nonconnectionLabel.text = @"网络不给力,刷新试试~";
            nonconnectionLabel.textColor = [UIColor whiteColor];
            [self.view addSubview:nonconnectionLabel];
            
            //动画效果,通知栏缓慢出现
            [UIView animateWithDuration:0.6 animations:^{
                nonconnectionLabel.frame = CGRectMake(0, 0, kWidth, 50);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.6 delay:3 options:UIViewAnimationOptionAutoreverse animations:^{
                    nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                    [nonconnectionLabel release];
                    
                } completion:^(BOOL finished) {
                    
                }];
                
                //            [UIView animateWithDuration:1.9 animations:^{
                //                nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                //            //[nonconnectionLabel removeFromSuperview];
                //                [nonconnectionLabel release];
                //            }];
                
            }];
            
        }
    });

    UIBarButtonItem *leftItemFirst = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-zuojiantou-3"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    
    UIBarButtonItem *leftItemSecond = [[UIBarButtonItem alloc]initWithTitle:self.leftString style:UIBarButtonItemStyleDone target:self action:nil];
    self.navigationItem.leftBarButtonItems = @[leftItemFirst,leftItemSecond];

    self.cookArray = [NSMutableArray array];
    
    [NewWorking GetDayaWithURL:[NSString stringWithFormat:@"http://food.boohee.com/fb/v1/categories?kind=%@",self.urlString]  dic:nil sussess:^(id responseObject) {
        
        
        for (NSDictionary *dic in responseObject[@"categories"]) {
            ClassModel *class = [[ClassModel alloc]init];
        [class setValuesForKeysWithDictionary:dic];
        [self.cookArray addObject:class];
        [class release];
        

        }
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 10;
        layout.itemSize = CGSizeMake((kWidth-40)/3, (kWidth-40)/3/2);
    self.cookView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:layout];
        self.cookView.backgroundColor = [UIColor clearColor];
    self.cookView.dataSource = self;
    self.cookView.delegate = self;
        self.cookView.bounces = NO;
        self.cookView.showsVerticalScrollIndicator = NO;
//        self.cookView.backgroundColor = [UIColor colorWithRed:0.961 green:0.961 blue:0.961 alpha:1];
    [self.view addSubview:self.cookView];
        [layout release];
    [_cookView release];
        
        [self.cookView registerClass:[CookCollectionViewCell class] forCellWithReuseIdentifier:@"cook"];

    } filed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
    
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.cookArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CookCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cook" forIndexPath:indexPath];
    
    cell.cook = self.cookArray[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

//跳转列表界面
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    StapleViewController *staple = [[StapleViewController alloc]init];
    staple.kindStr = self.urlString;
    staple.classModel = self.cookArray[indexPath.row];
    [self.navigationController pushViewController:staple animated:YES];
    
    [staple release];
    
}



-(void)backAction:(UIBarButtonItem *)item{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = YES;
    
}


- (BOOL)prefersStatusBarHidden{
    NSLog(@"ne");
    return NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
